package com.jing.service.impl;

import com.jing.mapper.ReadingRoomsMapper;
import com.jing.mapper.SensorMapper;
import com.jing.pojo.SensorConfig;
import com.jing.service.SensorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;


/**
 * @author 陈书憬
 * @version 1.0
 * 2024/9/28
 */
@Service
public class SensorServiceImpl implements SensorService {


    @Autowired
    private SensorMapper sensorMapper;

    @Autowired
    private ReadingRoomsMapper readingRoomsMapper;

//    @Override
//    public void add(SensorConfig sensorConfig) {
//        sensorConfig.setCreateTime(String.valueOf(LocalDateTime.now()));
//        sensorConfig.setStatus("true");
////        sensorMapper.add(sensorConfig);
//    }

    @Override
    public void add(String roomId, String roomName, int dhtPin, int firePin, int ledPin) {
        SensorConfig sensorConfig = new SensorConfig();


        sensorConfig.setRoomId(Integer.parseInt(roomId));
        sensorConfig.setRoomName(roomName);
        sensorConfig.setDhtPin(dhtPin);
        sensorConfig.setFirePin(firePin);
        sensorConfig.setLedPin(ledPin);

        String formattedDateTime = formatDateTime(LocalDateTime.now());
        sensorConfig.setCreateTime(formattedDateTime);

        readingRoomsMapper.updateIsSet(Long.valueOf(roomId), true);
        sensorMapper.add(sensorConfig);

    }

    @Override
    public void getIsSet(Integer id) {
        sensorMapper.getIsSet(id);
    }

    @Override
    public List<SensorConfig> selectRoom(Integer roomId) {
        List<SensorConfig> sensorConfigs = sensorMapper.selectRoom(roomId);
        return sensorConfigs;
    }

    @Override
    public void update(String roomId, int dhtPin, int firePin, int ledPin) {
        sensorMapper.update(roomId, dhtPin, firePin, ledPin);
    }

    @Override
    public List<SensorConfig> selectAllRoomsList() {
        List<SensorConfig> sensorConfigs = sensorMapper.selectAllRoomsList();
        return sensorConfigs;
    }


    private static String formatDateTime(LocalDateTime dateTime) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return dateTime.format(formatter);
    }
}
